'use strict'
const { Sequelize, Op } = require('sequelize');
const { getFliterFlies } = require('../utils/tools');
const {db_database,db_host,db_uid,db_pwd,db_dialect}=require('../config');

let db = new Sequelize(db_database, db_uid, db_pwd, {
    dialect: db_dialect,
    host: db_host
});


let obj = {};

let flies = getFliterFlies('./modles');

function defineModles(flies) {
    flies.forEach(element => {
        let tbShuxing = require('../modles/' + element.replace('.js', ''));
        let objName = element.replace('.js', '');
        let tbName = objName.toLowerCase();

        obj[objName] = db.define(tbName, tbShuxing, {
            tableName: 'app_' + tbName,
            charset:'utf8'
        });

    });

}

defineModles(flies);

obj.sync = async (force) => {
    if (force) {
        return db.sync({ force: true });
    }
    return db.sync();
}

module.exports = obj;